﻿using ProjectManager.Interface;
using ProjectManager.Logic;
using ProjectManager.Logic.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManager.Controller
{
    internal partial class LogicController : ILogic
    {
        public List<ProjectTaskInformation> GetTasks()
        {
            if (this.currentProject == null) return [];
            return sqlController.GetTasks(CurrentProject);
        }
        public void PublishTask(
            ProjectTaskInformation rootTask,
            TeamInformation teamInformation,
            DateTime StartDate,
            DateTime EndDate,
            string taskName,
            string taskDescription,
            List<string> protectedPaths
            )
        {
            if (!teamInformation.Leader.Equals(CurrentIdentity))
            {
                ShowErrorMessage?.Invoke("没有权限");
                return;
            }

            string taskPath = Path.Combine(CurrentProject.Path, taskName);
            Directory.CreateDirectory(taskPath);
            ProjectTaskInformation Task = new()
            {
                TeamID = teamInformation.ID,
                ID = sqlController.GetTasks(CurrentProject).GenerateTaskID(),
                BranchName = taskName,
                Path = taskPath,
                Name = taskName,
                Description = taskDescription,
                protectedPaths = protectedPaths,
                StartDate = StartDate,
                EndDate = EndDate,
                Status = ProjectTaskStatus.Working,
                ChildTask = []
            };

            gitController.CreateBranch(rootTask.Path, taskName);
            gitController.Clone(CurrentProject.Url, Task.Path, Task.BranchName);


            rootTask.ChildTask.Add(Task);
            sqlController.AddTask(CurrentProject, Task);
            sqlController.UpdateTask(CurrentProject, rootTask);
            CommitAndPush(CurrentProject.TaskPath, "Task", "Publish Task", taskName);
        }
        public void UpdateTask(ProjectTaskInformation rootTask, TeamInformation teamInformation, DateTime StartDate, DateTime EndDate, string taskName, string taskDescription, List<string> protectedPaths, ProjectTaskStatus taskStatus)
        {
            rootTask.Status = taskStatus;
            rootTask.StartDate = StartDate;
            rootTask.EndDate = EndDate;
            rootTask.protectedPaths = protectedPaths;
            rootTask.Name = taskName;
            rootTask.Description = taskDescription;

            sqlController.UpdateTask(CurrentProject, rootTask);
            CommitAndPush(CurrentProject.TaskPath, "Task", "Update Task", taskName);
        }
        public void SubmitTask(ProjectTaskInformation rootTask, string submitTitle, string submitDescription)
        {
            foreach (ProjectTaskInformation childTask in rootTask.ChildTask)
            {
                if (childTask.Status != ProjectTaskStatus.Succeeded)
                {
                    ShowErrorMessage?.Invoke("子任务仍未结束");
                    return;
                }
            }
            rootTask.Status = ProjectTaskStatus.WaitForAudit;
            rootTask.Description += $"\n==comment==\n==={submitTitle}===\n{submitDescription}\n";
            sqlController.UpdateTask(CurrentProject, rootTask);
            CommitAndPush(CurrentProject.TaskPath, "Task", "Submit Task", rootTask.Name);

            gitController.Commit(rootTask.Path, rootTask.BranchName, submitTitle, submitDescription);
            gitController.Push(rootTask.Path, rootTask.BranchName);
        }
        public void AuditTaskStart(ProjectTaskInformation rootTask, out List<string> beforePath, out List<string> completePath)
        {
            var _parentTask = rootTask.GetParent(sqlController.GetTasks(CurrentProject));
            var parentTask = _parentTask ?? throw new Exception("错误任务被审核");

            gitController.Pull(rootTask.Path, rootTask.BranchName);
            gitController.Pull(parentTask.Path, parentTask.BranchName);

            beforePath = rootTask.protectedPaths.Select(x => Path.Combine(parentTask.Path, x)).ToList();
            completePath = rootTask.protectedPaths.Select(x => Path.Combine(rootTask.Path, x)).ToList();
        }
        public void AuditTaskFinish(ProjectTaskInformation rootTask, bool succeed, string AuditTitle, string AuditDescription)
        {
            if (succeed)
            {
                ProjectTaskInformation baseTask = rootTask.GetParent(GetTasks()) ?? throw new Exception("LogicController: 不能审核根任务");
                try
                {
                    gitController.Merge(baseTask, rootTask);
                    rootTask.Status = ProjectTaskStatus.Succeeded;
                    gitController.Push(baseTask.Path, baseTask.BranchName);
                }
                catch (Exception ex)
                {
                    ShowErrorMessage?.Invoke("未能成功合并，请检查是否更改了保护路径之外的文件\n" + ex.Message);
                }
            }
            else
            {
                rootTask.Status = ProjectTaskStatus.Working;
            }
            rootTask.Description += $"\n==comment==\n==={AuditTitle}===\n{AuditDescription}\n";
            sqlController.UpdateTask(CurrentProject, rootTask);
            gitController.Push(CurrentProject.TaskPath, "Task");
        }
    }
}
